<template>
  <view class="cate-box">
    <!-- 自定义搜索组件 -->
    <my-search @click="gotoSearch"></my-search>

    <!-- 滑动区域 -->
    <view class="scroll-view">
      <!-- 左侧的盒子 -->
      <scroll-view class="scroll-view-left" scroll-y :style="{ height: vh + 'px' }">
        <view :class="['scrolll-view-left-item', index === active ? 'active' : '']" v-for="(item, index) in cateData" :key="item.cat_id" @click="changeActive(index)">
          {{ item.cat_name }}
        </view>
      </scroll-view>
      <!-- 右侧的盒子 -->
      <scroll-view class="scroll-view-right" scroll-y :style="{ height: vh + 'px' }" :scroll-top="scrollTop">
        <view v-for="(item, index) in secondCateData" :key="index" class="second-cate">
          <view class="second-cate-title">/ {{ item.cat_name }} /</view>
          <view class="three-cate">
            <view class="three-cate-item" v-for="(item1, index1) in item.children" :key="index1" @click="targetTo(item1)">
              <image :src="item1.cat_icon" mode=""></image>
              <text>{{ item1.cat_name }}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
import tabBarBadge from '@/mixins/tabBar-badge.js';
export default {
  mixins: [tabBarBadge],
  data() {
    return {
      vh: 0, //宽度样式
      cateData: [], //分类数据
      active: 0, //当前选中分类索引
      secondCateData: [], //二级分类数据
      scrollTop: 0 //右侧滚动区域下滚动距离
    };
  },
  methods: {
    async getCateData() {
      const { data: res } = await uni.$http.get('/api/public/v1/categories');
      if (res.meta.status !== 200) return uni.$showMsg();
      console.log(res);
      this.cateData = res.message;
      this.secondCateData = res.message[0].children;
    },
    changeActive(index) {
      this.active = index;
      this.secondCateData = this.cateData[index].children;
      this.scrollTop = this.scrollTop === 0 ? 1 : 0;
    },
    targetTo(item1) {
      uni.navigateTo({
        url: '/subpages/goods_list/goods_list?cid=' + item1.cat_id
      });
    },
    gotoSearch() {
      uni.navigateTo({
        url: '../../subpages/search/search'
      });
    }
  },
  created() {
    this.vh = uni.getSystemInfoSync().windowHeight - 38;
    this.getCateData();
  }
};
</script>

<style lang="scss">
.scroll-view {
  display: flex;
  .scroll-view-left {
    width: 120px;
    .scrolll-view-left-item {
      height: 60px;
      font-size: 12px;
      line-height: 60px;
      text-align: center;
      background-color: #f7f7f7;
      position: relative;
      &.active {
        background-color: #fff;
        &::before {
          content: '';
          display: block;
          height: 30px;
          width: 3px;
          background-color: #c00000;
          position: absolute;
          top: 50%;
          left: 0;
          transform: translateY(-50%);
        }
      }
    }
  }
}

.second-cate {
  padding: 15px 0;
  .second-cate-title {
    text-align: center;
    font-size: 14px;
    font-weight: bold;
  }
  .three-cate {
    display: flex;
    flex-wrap: wrap;
    .three-cate-item {
      margin: 15rpx 0;
      width: 33.3%;
      display: flex;
      flex-direction: column;
      align-items: center;
      image {
        width: 60rpx;
        height: 60rpx;
      }
      text {
        font-size: 24rpx;
      }
    }
  }
}
</style>
